import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/request.js';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: {},
		token: '',
		tabBarShow: true,
		Statephone: "",
		is_jgg: "",
		level: uni.getStorageSync("share_level") != undefined ? uni.getStorageSync("share_level") : ""
	},

	mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.token = provider.token;
			uni.setStorage({ //本地化存储token
				key: 'token',
				data: provider.token
			})
		},
		setUserInfo(state) {
			request({
				url: 'index/index/get_user_info',
				success: (res) => {
					if (res.code == 0) {
						// console.log("res.data",res.data)
						state.userInfo = {
							id: res.data.id,
							username: res.data.username,
							avatar: res.data.avatar,
							mobile: res.data.mobile,
							nickname: res.data.nickname,
							is_admin: res.data.is_admin,
						};
					} else {
						this.commit('logout')
					}
				}
			});
		},
		setUserMessage(state, provider) {
			state.level = provider.user_date.level;
			state.is_jgg = provider.user_date.is_jgg;
			state.Statephone = provider.wx_list.phoneNumber;
			state.tabBarShow = provider.user_date.is_not_included;
			console.log(state.tabBarShow)
			if (state.tabBarShow == 1) {
				state.tabBarShow = true
				uni.showTabBar()
			} else {
				state.tabBarShow = false
				uni.hideTabBar()
			}
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			state.token = '';
			uni.removeStorage({
				key: 'token'
			})
		},
		changeTabBar(state, phone) {
			console.log(phone)
			let to_phone = uni.getStorageSync("phone") != "" ? uni.getStorageSync("phone") : (phone
				.split('-')[
					0] != "undefined" ? phone.split('-')[
					0] : '');
			let change_level = uni.getStorageSync("level") != "" ? uni.getStorageSync("level") : (phone
				.split(
					'-')[1] != "undefined" ? phone.split(
					'-')[1] : '');
			console.log(to_phone, change_level)
			request({
				url: 'index/index/contact_us',
				data: {
					phone: to_phone,
					level: change_level
				},
				success: (res) => {
					if (res.data.is_not_included == 0) {
						uni.hideTabBar()
						state.tabBarShow = false
					} else {
						state.tabBarShow = true
					}
					state.is_jgg = res.data.is_jgg;
					state.level = res.data.level;
				},
				Error: (err) => {
					console.log(err)
				}
			})
		}
	},
	actions: {

	}
})

export default store